package cn.zhuama.basic.common;

import ch.qos.logback.core.util.FileUtil;
import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.FastDfsUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/common")
public class FileEditCommonController {
    @PostMapping("/logoUpload")
    public String logoUpload(@RequestPart(required = true,value = "file") MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);

        String upload = FastDfsUtils.upload(bytes, extension);
        return upload;
    }

    @DeleteMapping("/logoDelete")
    public AjaxResult logoDelete(@RequestParam("path")String path) {
        try {
            String[] paths=path.split("/",3);
            FastDfsUtils.delete(paths[1],paths[2]);
            return AjaxResult.me().setSuccess(true);
        } catch (RuntimeException e) {
            return AjaxResult.me().setSuccess(false);
        }
    }
}
